950C - Zebras - CodeForces Solution


constructive algorithms greedy *1600

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;
queue<int>rele;
queue<int>bune;
vector<int>vec[200005];
int main()
{
    int n,i,j,k,l,t,op,a,b,m,x,st,dr,v,c,curr;
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    t=1;
    string s;
    cin>>s;
    n=s.size();
    int cnt=0,imp=0;
    for(i=0; i<n; i++)
    {
        if(s[i]=='0')
        {
            if(rele.size()==0)
            {
                cnt++;
                vec[cnt].push_back(i+1);
                bune.push(cnt);
            }
            else
            {
                curr=rele.front();
                rele.pop();
                vec[curr].push_back(i+1);
                bune.push(curr);
            }
        }
        else
        {
            if(bune.size()==0)
            {
                imp=1;
            }
            else
            {
                curr=bune.front();
                bune.pop();
                vec[curr].push_back(i+1);
                rele.push(curr);
            }
        }
    }
    if(rele.size())
    {
        imp=1;
    }
    if(imp==1)
    {
        cout<<-1;
    }
    else
    {
        cout<<cnt<<endl;
        for(i=1;i<=cnt;i++)
        {
            cout<<vec[i].size()<<" ";
            for(j=0;j<vec[i].size();j++)
            {
                cout<<vec[i][j]<<" ";
            }
            cout<<'\n';
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments